package com.example.myapplication3;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication3.bean.Beijing;
import com.example.myapplication3.bean.Student1;
import com.example.myapplication3.bean.Student2;
import com.example.myapplication3.bean.Student3;
import com.example.myapplication3.bean.Student4;
import com.example.myapplication3.interfaces.IHome;
import com.example.myapplication3.persenter.HomePersenter;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,IHome.View{

    private Button an1;
    private Button an2;
    private RecyclerView rv;
    private ArrayList<Student1.DataBean> list;
    private HomePersenter homePersenter;
    private ArrayList<Student2.DataBean> list2;
    private ArrayList<Student3.DataBean> list3;
    private ArrayList<Student4.DataBean> list4;
    private Adapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initView();

    }

    private void initData() {
        homePersenter = new HomePersenter(this);
        homePersenter.getCity1();


    }

    private void initView() {
        an1 = (Button) findViewById(R.id.an1);
        an1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        an2 = (Button) findViewById(R.id.an2);
        rv = (RecyclerView) findViewById(R.id.rv);
        an1.setOnClickListener(this);
        an2.setOnClickListener(this);


        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.addItemDecoration(new DividerItemDecoration(this,1));

        list = new ArrayList<>();
        list2 = new ArrayList<>();
        list3 = new ArrayList<>();
        list4 = new ArrayList<>();

        adapter = new Adapter(list,this);
        rv.setAdapter(adapter);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.an1:
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                startActivityForResult(intent,22);
                break;
            case R.id.an2:
                Toast.makeText(this, "sdfg", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @Override
    public void getStudent1(Student1 result) {

        if(list!=null){
            list.clear();
        }
        Log.e("TAG", result.getData().get(0).getCity());
        list.addAll(result.getData());

        HashMap<String, String> mapp = new HashMap<>();
        mapp.put("station_code",String.valueOf(result.getData().get(0).getStation_code()));
        homePersenter.getCity4(mapp);

        Log.e("TAG",result.getData().get(0).getCity());
        HashMap<String, String> map = new HashMap<>();
        map.put("station_code",String.valueOf(result.getData().get(0).getStation_code()));
        homePersenter.getCity2(map);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void getStudent2(Student2 result) {
        Log.e("TAG",result.getData().getCity());
        HashMap<String,String> map=new HashMap<>();
        map.put("city",String.valueOf(result.getData().getCity()));
        homePersenter.getCity3(map);
    }

    @Override
    public void getStudent3(Student3 result) {
        Log.e("TAG",result.getData().getLevel());


    }

    @Override
    public void getStudent4(Student4 result) {
        Log.e("TAG77",result.getData().getCity());

    }

    @Override
    public void getStudent5(Beijing result) {

    }

    @Override
    public void tips(String tip) {
        Toast.makeText(this, tip, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void loading(int visible) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String name = data.getStringExtra("name");
        an1.setText(name);
    }
}
